#ifndef TOAST_H
#define TOAST_H

#include <QWidget>
#include <QLabel>
#include <QTimer>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QPointer>
#include <QMap>

namespace Ui {
class Toast;
}

class Toast : public QWidget
{
    Q_OBJECT
public:
    explicit Toast(const QString& msg,
                   const QIcon& icon,
                   const QColor& bgColor,
                   int durationMs,
                   Qt::Alignment position,
                   QWidget* parent = nullptr);
    ~Toast();

    static void setToastParent(QWidget* parent);

    static Toast* info(const QString& msg,
                       int durationMs=2000,
                       Qt::Alignment position=Qt::AlignBottom | Qt::AlignCenter);

    static Toast* warning(const QString& msg,
                          int durationMs=2000,
                          Qt::Alignment position=Qt::AlignBottom | Qt::AlignCenter);

    static Toast* error(const QString& msg,
                        int durationMs=2000,
                        Qt::Alignment position=Qt::AlignBottom | Qt::AlignCenter);

    static Toast* success(const QString& msg,
                          int durationMs=2000,
                          Qt::Alignment position=Qt::AlignBottom | Qt::AlignCenter);

protected:
    void showEvent(QShowEvent* event) override;

private slots:
    void fadeOut();

private:
    Ui::Toast* ui;

    QTimer* timer;
    QPropertyAnimation* opacityAnim;
    QPropertyAnimation* moveAnim;
    QParallelAnimationGroup* animationGroup;

    int duration;
    Qt::Alignment align;

    static QWidget* toastParent;
    static QMap<Qt::Alignment, QList<QPointer<Toast>>> activeToasts;

    void repositionToasts();
};

#endif // TOAST_H
